package com.lemon.tuvi2015.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lemon.tuvi2015.R;
import com.lemon.tuvi2015.object.GlobalObject;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

public class VankhanAdapter extends BaseAdapter {

	Context context;
	ArrayList<GlobalObject> listZodiac;
	int color=-1;
	boolean isSms=false;
	public VankhanAdapter(Context context, ArrayList<GlobalObject> listZodiac) {
		this.context = context;
		this.listZodiac = listZodiac;
	}
	//Picasso.with(context).load(tnu).error(R.drawable.no_img).placeholder(R.drawable.img_def).into(holder.immoPic);
	public VankhanAdapter(Context context, ArrayList<GlobalObject> listZodiac,int color) {
		this.context = context;
		this.listZodiac = listZodiac;
		this.color=color;
	}
	public VankhanAdapter(Context context, ArrayList<GlobalObject> listZodiac,int color,boolean isSms) {
		this.context = context;
		this.listZodiac = listZodiac;
		this.color=color;
		this.isSms=true;
	}
	@Override
	public int getCount() {
		return listZodiac.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.vankhan_item, null);
			holder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.congiap_item);
			holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
			holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
			holder.imageViewNext = (ImageView) convertView.findViewById(R.id.vk_next);
			holder.textView = (TextView) convertView.findViewById(R.id.textView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final GlobalObject zodiacObject = listZodiac.get(position);
		Picasso.with(context).load(zodiacObject.getIMAGE()).into(holder.imageView, new Callback() {
			@Override
			public void onSuccess() {
				holder.progressBar.setVisibility(View.GONE);
			}

			@Override
			public void onError() {
				holder.progressBar.setVisibility(View.GONE);
				if(color>-1)
					if(!isSms)
						holder.imageView.setImageResource(R.drawable.icon_star);
					else
						holder.imageView.setImageResource(R.drawable.heart);
				else
					holder.imageView.setImageResource(R.drawable.icon_vk);	
			}
				
		});
			
		if (color>-1) {
			holder.textView.setTextColor(Color.BLACK);
			holder.imageViewNext.setVisibility(View.VISIBLE);
		}
		holder.textView.setText(zodiacObject.getNAME());
		return convertView;
	}

	private class ViewHolder {
		RelativeLayout relativeLayout;
		ImageView imageView,imageViewNext;
		TextView textView;
		ProgressBar progressBar;
	}
}
